package com.youlu.campus.admin.ca.controller;

import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.VO.req.CreateCatemplateVO;
import com.youlu.campus.entity.VO.req.QueryCatemplateVO;
import com.youlu.campus.service.ca.CaTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class CaTemplateController {

    @Autowired
    private CaTemplateService caTemplateService;

    /**
     * 新增模版
     *
     * @param createCatemplate
     * @param bindingResult
     * @return
     */
    @PostMapping("/ca/template/add-or-edit")
    public RestResult addOrUpdate(@RequestBody @Validated CreateCatemplateVO createCatemplate,
                                  BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RestResultBuilder.builder().failure(bindingResult.getFieldError().getDefaultMessage()).build();
        }
        caTemplateService.save(createCatemplate);
        return RestResultBuilder.builder().success().build();
    }

    /**
     * 模版列表
     *
     * @param queryCatemplate
     * @return
     */
    @PostMapping("/ca/template/list")
    public RestResult list(@RequestBody QueryCatemplateVO queryCatemplate) {
        return RestResultBuilder.builder().success().data(caTemplateService.list(queryCatemplate)).build();
    }

    /**
     * 模版详情
     *
     * @param activityId
     * @return
     */
    @GetMapping("/ca/template/activity")
    public RestResult activityId(@RequestParam("activityId") String activityId) {
        return RestResultBuilder.builder().success().data(caTemplateService.findByActivityId(activityId)).build();
    }

    /**
     * 模版详情
     *
     * @param id
     * @return
     */
    @GetMapping("/ca/template/detail")
    public RestResult detail(@RequestParam("id") String id) {
        return RestResultBuilder.builder().success().data(caTemplateService.detail(id)).build();
    }

    /**
     * 模版copy
     *
     * @param id
     * @return
     */
    @GetMapping("/ca/template/copy")
    public RestResult copy(@RequestParam("id") String id) {
        caTemplateService.caCaTemplateCopyById(id);
        return RestResultBuilder.builder().success().data(null).build();
    }



}
